<?php 
namespace Admin\Controller; 
use Think\Controller;
class ArticleController extends CommonController{
	//博文列表
	public function index(){
		$this->article = D("Article")->getArticles();
		$this->display();
	}
	//添加博文
 	public function addArticle(){
 		//博文分类
 		$cate = M('cate')->order("sort ASC")->select();;
 		$this->cate = \Util\Category::unlimitedForLevel($cate);
 		//博文属性
 		$this->attr = M('attr')->select();
 		$this->display();
 	}
	//添加博文处理表单
 	public function addArticleHandle(){
 		if(!IS_POST) $this->error("提交方式错误");
		$data = array(
			'title' => $_POST['title'],
			'content' => $_POST['content'],
			'time' => time(),
			'click' => (int)$_POST['click'],
			'cate_id' => (int)$_POST['cate_id'],
		); 	
		if($artcle_id = M('article')->add($data)){
			if(isset($_POST['attr_id'])){
				$sql = 'INSERT INTO ' . C('DB_PREFIX') . 'article_attr (article_id, attr_id) VALUES';
				foreach($_POST['attr_id'] as $v){
					$sql .= '(' . $artcle_id . ',' . $v . '),';
				}
				$sql = rtrim($sql, ',');
				M('article_attr')->query($sql);
			}
			$this->success('添加成功');
		}else{
			$this->error('添加失败');
		}	
 	}
 	//添加图片上传处理
 	public function upload(){	
		$config = array(
			'maxSize'           =>  2000000,    // 上传文件的最大值
			'supportMulti'      =>  true,    // 是否支持多文件上传
			'allowExts'         =>  array('jpg','png','gif','bmp','jpeg'),    // 允许上传的文件后缀 留空不作后缀检查
			'autoSub'           =>  true,// 启用子目录保存文件
			'subName'    		=>    array('date','Ymd'), //子目录创建方式，采用数组或者字符串方式定义
			'savePath'          =>  '/',// 上传文件保存路径
		); 	
		$upload = new \Think\Upload($config);// 实例化上传类
		$info   =   $upload->upload();
		if($info){//上传成功
			//储存每张图片的信息 
			foreach ($info as $file) {
				//echo './Uploads'.$file['savepath'].$file['savename'];
				\Util\Image::water('./Uploads'.$file['savepath'].$file['savename']);
				echo json_encode(
					array(
						'url' => $file['savepath'].$file['savename'],
						'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES),
						'original' => $file['name'],
						'state' => 'SUCCESS',
					)
				);
			}
		 }else{//上传失败
			echo json_encode(array('state' => $upload->getError()));
		 }		

 	}
	//回收站
 	public function trash(){
 		$this->article = D("Article")->getArticles(1);
 		$this->display();
 	}
 	//移至回收站
 	public function trashHandle(){
 
 		$data['id']  = I('id','','intval');
 		$data['del'] = 1;
 		if(M('article') ->save($data)){
 			$this->success("删除成功");
 		}else{
 			$this->error("删除失败");
 		}
 	}
 	//彻底清除博文
 	public function emptyTrashHandle(){
 	
 	 	$id = $data['id']  = I('id','','intval');
 		if(M('article') ->delete($id)){
 			M('article_attr')->where(array('article_id'=>$id))->delete();
 			$this->success("删除成功");
 		}else{
 			$this->error("删除失败");
 		}	
 	}
 	//还原博文
 	public function restoreHandle(){
 		
 		$data['id']  = I('id','','intval');
 		$data['del'] = 0;
 		if(M('article') ->save($data)){
 			$this->success("还原成功");
 		}else{
 			$this->error("还原失败");
 		}
 	} 	
}
?>